package one.bugu.android.demon.util;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

public abstract class BaseViewAdapter<T> extends BaseAdapter {
    /*数据源*/
    protected List<T> mListData;
    /*上下文*/
    protected Context mContext;
    /*布局ID*/
    private int mLayoutID = -1;
    /*点击事件*/
    private OnClickListener mClickListener;
    /*单击事件和长单击事件的屏蔽标识*/
    private boolean clickFlag;

    public BaseViewAdapter(Context mContext, int mLayoutID, List<T> mListData) {
        this.mListData = mListData;
        this.mContext = mContext;
        this.mLayoutID = mLayoutID;
    }

    public BaseViewAdapter(Context mContext, int mLayoutID) {
        this.mContext = mContext;
        this.mLayoutID = mLayoutID;
    }

    /*设置数据源*/
    public void setData(List<T> mListData) {
        this.mListData = mListData;
        notifyDataSetChanged();
    }

    /*在列表最后位置添加一条数据*/
    public void addData(T t) {
        if (mListData != null) {
            mListData.add(t);
        }
        notifyDataSetChanged();
    }

    /*在指定位置添加一条数据*/
    public void addData(int postition, T t) {
        if (mListData != null) {
            mListData.add(postition, t);
        }
        notifyDataSetChanged();
    }

    /*更新数据*/
    public void upData(List<T> mListData) {
        if (mListData != null) {
            mListData.clear();
        }
        setData(mListData);
    }

    /*移除某条数据*/
    public void removeData(T t) {
        if (mListData != null) {
            mListData.remove(t);
        }
        notifyDataSetChanged();
    }

    /*移除指定位置的数据*/
    public void removeData(int position) {
        if (mListData != null) {
            mListData.remove(position);
        }
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mListData == null ? 0 : mListData.size();
    }

    @Override
    public T getItem(int position) {
        return mListData == null ? null : mListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View inflate = LayoutInflater.from(mContext).inflate(mLayoutID, parent, false);
        BaseViewHolder viewHolder = new BaseViewHolder(inflate, mContext);
        convert(viewHolder, mListData.get(position), position);
        /*单击事件回调*/
        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickFlag) {
                    if (mClickListener != null) {
                        mClickListener.OnItemClick(v, position);
                    }
                }
                clickFlag = true;
            }
        });
        /*单击长按事件回调*/
        inflate.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mClickListener != null) {
                    mClickListener.OnItemLongClick(v, position);
                }
                clickFlag = false;
                return false;
            }
        });
        return inflate;
    }

    protected abstract void convert(BaseViewHolder baseViewHolder, T t, int position);

    public void setClickListener(OnClickListener mClickListener) {
        this.mClickListener = mClickListener;
    }

    public interface OnClickListener {
        void OnItemClick(View view, int position);

        void OnItemLongClick(View view, int position);
    }
}
